package com.wbao.dianniu.manager;

import android.content.Context;

import com.wbao.dianniu.data.QuestV1Data;
import com.wbao.dianniu.http.Batch;
import com.wbao.dianniu.http.Command;
import com.wbao.dianniu.http.HttpUtil;
import com.wbao.dianniu.listener.IQuestListV1;
import com.wbao.dianniu.listener.IQuestListV1Listener;
import com.wbao.dianniu.logical.Action;
import com.wbao.dianniu.logical.GlobalContext;
import com.wbao.dianniu.utils.JsonUtil;

import org.apache.http.entity.StringEntity;

import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * Created by yejianjun on 2018/11/12.
 */

public class QuestListV1Manager implements IQuestListV1 {

    private IQuestListV1Listener mListener = null;
    private Context mContext;

    public QuestListV1Manager(Context context) {
        mContext = context;
    }


    @Override
    public void questList(int type, String lastUpdateDate, String refresh) {
        StringEntity stringEntity = null;
        try {
            Batch batch = new Batch();
            batch.setToken(GlobalContext.getCacheToken());
            Command command = new Command();
            command.addAction(Action.ACCOUNT_QUEST_LIST_V1)
                    .addArgs("type", type)
                    .addArgs("refresh", refresh)
                    .addArgs("lastUpdateDate", lastUpdateDate);
            batch.addCommand(command);
            stringEntity = new StringEntity(batch.toJson(), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        HttpUtil http = new HttpUtil();
        http.req(mContext, stringEntity, new HttpUtil.MyResponse() {

            @Override
            public void responseSucess(Object obj) {
                // TODO Auto-generated method stub
                QuestV1Data foosList = JsonUtil.toObject(obj, QuestV1Data.class);
                // 通知界面
                if (null != mListener)
                    mListener.onQuestListV1Success(foosList);
            }

            @Override
            public void responseFailure(int errorCode, String msg) {
                // TODO Auto-generated method stub
                if (null != mListener)
                    mListener.onQuestListV1Failure(errorCode, msg);
            }
        });
    }

    @Override
    public boolean addQuestListV1Listener(IQuestListV1Listener listener) {
        mListener = listener;
        return mListener != null;
    }

    @Override
    public boolean removeQuestListV1Listener(IQuestListV1Listener listener) {
        if (null == listener)
            return false;
        mListener = null;
        return true;
    }
}
